unit uFrameLookupModel;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  uDMUtilsdao, Vcl.Graphics, Vcl.Controls, uObjectAction, Vcl.Forms, DB, DBClient, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Mask, Vcl.DBCtrls;

type
  TFrameLookupModel = class(TFrame)
    edtID: TDBEdit;
    edtDC: TDBEdit;
    btnSearch: TButton;
    lblTitle: TLabel;
    procedure edtDCExit(Sender: TObject);
    procedure edtIDExit(Sender: TObject);
    procedure btnSearchClick(Sender: TObject);
    procedure FrameResize(Sender: TObject);
  private
    FFieldID, FFIeldDC: TFieldDataLink;
    FObjectActionSearch: TObjectAction;
    FObjectActionOrigem: TObjectAction;
    FCanLoadData: Boolean;
    FOldValueDC: String;
    FOldValueID: Integer;
  protected
    procedure UpdateOldValue;
    function ChangeValue: Boolean;
    procedure LoadData;
    procedure SetOldValueDC(const Value: String); Virtual;
    procedure SetOldValueID(const Value: Integer); Virtual;
    function GetOldValueDC: String; Virtual;
    function GetOldValueID: Integer; Virtual;
    function GetCanLoadData: Boolean; Virtual;
    procedure SetCanLoadData(const Value: Boolean); Virtual;
    procedure SetDataSource(const Value: TDataSource); Virtual;
    procedure SetFieldDC(const Value: String); Virtual;
    procedure SetFieldID(const Value: String); Virtual;
    procedure SetObjectActionOrigem(const Value: TObjectAction); Virtual;
    procedure SetObjectActionSearch(const Value: TObjectAction); Virtual;
    procedure SetTitle(const Value: String); Virtual;
    function GetDataSource: TDataSource; Virtual;
    function GetFieldDC: String; Virtual;
    function GetFieldID: String; Virtual;
    function GetTitle: String; Virtual;
    function GetObjectActionOrigem: TObjectAction; Virtual;
    function GetObjectActionSearch: TObjectAction; Virtual;
    { Private declarations }
  public
    { Public declarations }
    procedure Clear; Virtual;
    procedure UpdateSize(pbcanResizeForm: Boolean = True); Virtual;
    constructor Create(AOwner: TComponent); Override;
    destructor Destroy; Override;
    property ObjectActionOrigem: TObjectAction read GetObjectActionOrigem write SetObjectActionOrigem;
    property ObjectActionSearch: TObjectAction read GetObjectActionSearch write SetObjectActionSearch;

    property OldValueID: Integer read GetOldValueID write SetOldValueID;
    property OldValueDC: String read GetOldValueDC write SetOldValueDC;
  published
    property Title: String read GetTitle write SetTitle;
    property DataSource: TDataSource read GetDataSource write SetDataSource default nil;
    property DataField: String read GetFieldID write SetFieldID;
    property DataFieldDC: String read GetFieldDC write SetFieldDC;
    property CanLoadData: Boolean read GetCanLoadData write SetCanLoadData default True;
  end;

implementation

{$R *.dfm}

uses uDMUtils, uFactoryObject, uFormAddModel;

{ TFrameLookupModel }

procedure TFrameLookupModel.btnSearchClick(Sender: TObject);
begin
  LoadData;
  TFactoryObject.OpenSearch(FObjectActionSearch, FObjectActionOrigem, DataField, DataFieldDC);
  UpdateOldValue;
end;

function TFrameLookupModel.ChangeValue: Boolean;
begin
  Result  := ((Assigned(edtID.DataSource) and Assigned(edtID.DataSource.DataSet) and (FOldValueID <> edtID.DataSource.DataSet.FieldByName(edtID.DataField).AsInteger)) or
    (Assigned(edtDC.DataSource) and Assigned(edtDC.DataSource.DataSet) and (FOldValueDC <> edtDC.DataSource.DataSet.FieldByName(edtDC.DataField).AsString)));
end;

constructor TFrameLookupModel.Create(AOwner: TComponent);
begin
  ObjectActionOrigem := nil;
  ObjectActionSearch := nil;
  FOldValueDC := '';
  FOldValueID := 0;
  FCanLoadData := True;
  FFieldID := TFieldDataLink.Create;
  FFieldID.Control := self;
  FFIeldDC := TFieldDataLink.Create;
  FFIeldDC.Control := self;
  inherited;
end;

destructor TFrameLookupModel.Destroy;
begin
  ObjectActionOrigem := nil;
  ObjectActionSearch := nil;
  TDMUtils.DestroyObject(FFieldID);
  TDMUtils.DestroyObject(FFIeldDC);
  inherited;
end;

procedure TFrameLookupModel.Clear;
begin
  TDMUtilsDao.SetValorField(edtDC.DataSource.DataSet.FindField(edtDC.DataField), '');
  TDMUtilsDao.SetValorField(edtID.DataSource.DataSet.FindField(edtID.DataField), '');
  UpdateOldValue;
end;

procedure TFrameLookupModel.edtDCExit(Sender: TObject);
begin
  if not ChangeValue then
    Exit;

  if TDMUtils.IsEmpty(edtDC.DataSource.DataSet.FieldByName(edtDC.DataField).AsString) then
  begin
    Clear;
    Exit;
  end;

  LoadData;
  TFactoryObject.UpdateLookupSearch(FObjectActionSearch, FObjectActionOrigem, DataField, DataFieldDC, False);
  UpdateOldValue;
end;

procedure TFrameLookupModel.edtIDExit(Sender: TObject);
begin
  if not ChangeValue then
    Exit;

  if TDMUtils.IsEmpty(edtID.DataSource.DataSet.FieldByName(edtID.DataField).AsInteger) then
  begin
    Clear;
    Exit;
  end;

  LoadData;
  TFactoryObject.UpdateLookupSearch(FObjectActionSearch, FObjectActionOrigem, DataField, DataFieldDC);
  UpdateOldValue;
end;

procedure TFrameLookupModel.FrameResize(Sender: TObject);
var
  nWidth: Integer;
begin
  nWidth := btnSearch.Left + btnSearch.Width + 4;
  edtDC.Width := edtDC.Width + (Self.Width - nWidth);
  UpdateSize(False);
end;

function TFrameLookupModel.GetCanLoadData: Boolean;
begin
  Result := FCanLoadData;
end;

function TFrameLookupModel.GetDataSource: TDataSource;
begin
  Result := FFieldID.DataSource;
end;

function TFrameLookupModel.GetFieldDC: String;
begin
  Result := FFieldDC.FieldName;
end;

function TFrameLookupModel.GetFieldID: String;
begin
  Result := FFieldID.FieldName;
end;

function TFrameLookupModel.GetObjectActionOrigem: TObjectAction;
begin
  Result := FObjectActionOrigem;
end;

function TFrameLookupModel.GetObjectActionSearch: TObjectAction;
begin
  Result := FObjectActionSearch;
end;

function TFrameLookupModel.GetOldValueDC: String;
begin
  Result := FOldValueDC;
end;

function TFrameLookupModel.GetOldValueID: Integer;
begin
  Result := FOldValueID;
end;

function TFrameLookupModel.GetTitle: String;
begin
  Result := lblTitle.Caption;
end;

procedure TFrameLookupModel.LoadData;
var
  bOldCanAutoLoad: Boolean;
begin
  if not CanLoadData then
    Exit;

  CanLoadData := False;
  if FObjectActionSearch.FObjectDao.ExistData then
    Exit;
  bOldCanAutoLoad := FObjectActionSearch.CanAutoLoad;
  try
    FObjectActionSearch.CanAutoLoad := True;
    FObjectActionSearch.Load;
  finally
    FObjectActionSearch.CanAutoLoad := bOldCanAutoLoad;
  end;
end;

procedure TFrameLookupModel.SetCanLoadData(const Value: Boolean);
begin
  FCanLoadData := Value;
end;

procedure TFrameLookupModel.SetDataSource(const Value: TDataSource);
begin
  FFieldID.DataSource := Value;
  FFIeldDC.DataSource := Value;
  edtID.DataSource := Value;
  edtDC.DataSource := Value;
end;

procedure TFrameLookupModel.SetFieldDC(const Value: String);
begin
  FFieldDC.FieldName := Value;
  edtDC.DataField := Value;
end;

procedure TFrameLookupModel.SetFieldID(const Value: String);
begin
  FFieldID.FieldName := Value;

  if TDMUtils.IsEmpty(Title) or
    (Assigned(edtID.DataSource) and Assigned(edtID.DataSource.DataSet) and
    (Title = edtID.DataSource.DataSet.FieldByName(edtID.DataField).DisplayLabel)) then
    Title := edtID.DataSource.DataSet.FieldByName(Value).DisplayLabel;

  edtID.DataField := Value;
end;

procedure TFrameLookupModel.SetObjectActionOrigem(const Value: TObjectAction);
begin
  FObjectActionOrigem := Value;
  if not Assigned(FObjectActionOrigem) then
    Exit;
  DataSource := FObjectActionOrigem.FObjectDao.DSDados;
end;

procedure TFrameLookupModel.SetObjectActionSearch(const Value: TObjectAction);
begin
  FObjectActionSearch := Value;

  if ((not Assigned(ObjectActionOrigem)) and Assigned(Owner) and (Owner is TFormAddModel)) then
    ObjectActionOrigem := TFormAddModel(Owner).FObjectAction;

  if not Assigned(FObjectActionSearch) then
    Exit;

  if TDMUtils.IsEmpty(DataField) then
    DataField := FObjectActionSearch.FObjectDao.FieldID.FieldName;
  if TDMUtils.IsEmpty(DataFieldDC) then
  begin
    DataFieldDC := 'CC_' + FObjectActionSearch.FObjectDao.FieldDC.FieldName;
    if Assigned(ObjectActionOrigem) then
    begin
      if not Assigned(ObjectActionOrigem.FObjectDao.CDSDados.FindField(DataField)) then
      begin
        edtDC.DataSource := FObjectActionSearch.FObjectDao.CDSDados.DataSource;
        DataFieldDC := FObjectActionSearch.FObjectDao.FieldDC.FieldName;
      end;
    end;
  end;
end;

procedure TFrameLookupModel.SetOldValueDC(const Value: String);
begin
  FOldValueDC := Value;
end;

procedure TFrameLookupModel.SetOldValueID(const Value: Integer);
begin
  FOldValueID := Value;
end;

procedure TFrameLookupModel.SetTitle(const Value: String);
begin
  lblTitle.Caption := Value;
  UpdateSize;
end;

procedure TFrameLookupModel.UpdateOldValue;
begin
  FOldValueID := edtID.DataSource.DataSet.FieldByName(edtID.DataField).AsInteger;
  FOldValueDC := edtDC.DataSource.DataSet.FieldByName(edtDC.DataField).AsString;
end;

procedure TFrameLookupModel.UpdateSize(pbcanResizeForm: Boolean);
var
  nNewWidth: Integer;
begin
  edtID.Left := lblTitle.Left + lblTitle.Width + 6;
  edtDC.Left := edtID.Left + edtID.Width + 6;
  btnSearch.Left := edtDC.Left + edtDC.Width + 6;
  nNewWidth := btnSearch.Left + btnSearch.Width + 4;
  if pbcanResizeForm and (self.Width <> nNewWidth) then
    Self.Width := nNewWidth;
end;

initialization
RegisterClass(TFrameLookupModel);
end.
